export default class Queue {
  constructor() {
    this.count = 0 // 控制队列的大小
    this.lowestCount = 0 // 追踪第一个元素
    this.items = {}
  }

  /**
   * 向队列添加元素
   * @param {*} element
   */
  enqueue(element) {
    this.items[this.count] = element
    this.count++
  }

  /**
   * 从队列移除元素
   * @return
   */
  dequeue() {
    if (this.isEmpty()) {
      return undefined
    }
    const result = this.items[this.lowestCount]
    delete this.items[this.lowestCount]
    this.lowestCount++
    return result
  }

  /**
   * 查看队列头元素
   * @return
   */
  peek() {
    if (this.isEmpty) {
      return undefined
    }
    return this.items[this.lowestCount]
  }

  /**
   * 如果队列中不包含任何元素，返回 true，否则返回 false
   * @return {boolean}
   */
  isEmpty() {
    return this.size() === 0
  }

  clear() {
    this.items = {}
    this.count = 0
    this.lowestCount = 0
  }

  /**
   * 返回队列包含的元素个数，与数组的 length 属性类似
   * @return {number}
   */
  size() {
    return this.count - this.lowestCount
  }

  toString() {
    if (this.isEmpty()) {
      return ''
    }
    let objString = `${this.items[this.lowestCount]}`
    for (let i = this.lowestCount + 1; i < this.count; i++) {
      objString = `${objString},${this.items[i]}`
    }
    return objString
  }
}
